![]() |
PATH![]() |
![]() ![]() |
The Set command can function as an AppleScript command or an application command. The AppleScript command assigns one or more values to one or more variables. It can also be used to share data among lists, records, or script objects--see the Notes section below and Data Sharing. The application command sets the values of one or more objects.
You can use the Set command to set a variable to any value:
set x to 5
--result: 5
set myList to { 1, 2, "four" }
--result: {1, 2, "four"}
tell application "AppleWorks"
set x to word 1 of text body of front document
end tell
These two statements are equivalent:
set x to 3
3 returning x
--result: 3
Similarly, the following examples are equivalent:
tell front document of application "AppleWorks"
set x to word 1 of text body
end tell
tell front document of application "AppleWorks"
word 1 of text body returning x
end tell
In addition to setting a variable to a single value, you can set patterns of variables to patterns of values. For example, this script sets a list of two variables to the position of the front window.
tell application "Finder"
set {x, y} to position of front window
end tell
Since the Finder returns position of front window as a list of two integers, the preceding example sets x to the first item in the list and y to the second item.
Patterns set with the Set command can also be more complex. Here are some examples:
set x to {8, 94133, {firstName:"John", lastName:"Chapman"}}
set {p, q, r} to x
(* now p, q, and r have these values:
p = 8
q = 94133
r = {firstName:"John", lastName:"Chapman"} *)
set {p, q, {lastName:r}} to x
(* now p, q, and r have these values: p = 8
q = 94133
r = "Chapman" *)
As the last example demonstrates, the properties of a record need not be given in the same order and need not all be used when you set a pattern to a pattern, as long as the patterns match.
The use of the Set command with patterns is similar to the use of patterned parameters with subroutines, which is described in Subroutines With Positional Parameters.
If you use the Set command to set a variable to a list, record, or script object, the variable shares data with the original list, record, or script object. If you change the data of the original, the value of the variable also changes. Here's an example of how this works:
set myList to { 1, 2, 3 }
set yourList to myList
set item 1 of myList to 4
The result of these statements is that item 1 of both myList and yourList is 4 .
Data sharing promotes efficiency when using large data structures. Rather than making copies of shared data, the same data can belong to multiple structures. When one structure is updated, the others are automatically updated.
IMPORTANT
To avoid data sharing for lists, records, and script objects, you typically copy these items with the AppleScript command Copy, rather than use the Set command.
Only data in lists, records, and script objects can be shared; you cannot share other values. Moreover, you can share data only on the same computer, and the shared structures must all be in the same script.